<template>
  <div>
    <catalogue :screen="screen" />
    <el-card style="margin-top: 20px">
      <div v-show="screen == 0">
        <el-button
          type="primary"
          @click="insert"
          display="true"
          :disabled="cateStore.c3Id == ''"
          icon="Plus"
        >
          新增属性
        </el-button>
        <!-- 表格 -->
        <el-table border style="margin-top: 20px" :data="attrs">
          <el-table-column
            label="序号"
            width="80px"
            type="index"
            align="certer"
          ></el-table-column>
          <el-table-column
            label="属性名称"
            width="120px"
            prop="attrName"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template #="{ row, $index }">
              <el-tag
                class="ml-2"
                style="margin-right: 5px"
                v-for="(item, index) in row.attrValueList"
                :key="item.id"
                v-show="item.valueName"
              >
                {{ item.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180px">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                @click="update(row)"
              >
                编辑
              </el-button>
              <el-popconfirm
                :title="`确定删除${row.attrName}`"
                @confirm="deleteAttr(row.id)"
              >
                <template #reference>
                  <el-button type="primary" size="small" icon="Delete">
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="screen == 1">
        <el-form :inline="true">
          <el-form-item label="属性名称">
            <el-input v-model="attrVal.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="Plus"
          :disabled="attrVal.attrName == ''"
          @click="addAttrValue"
        >
          添加属性值
        </el-button>
        <el-button
          type="primary"
          @click="removeAll"
          :disabled="attrVal.attrValueList.length == 0"
        >
          全部删除
        </el-button>

        <el-table border style="margin: 10px 0px" :data="attrVal.attrValueList">
          <el-table-column
            label="序号"
            width="80px"
            align="certer"
            type="index"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template #="{ row, $index }">
              <el-input
                v-model="attrVal.attrValueList[$index].valueName"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="属性值操作">
            <template #="{ row, $index }">
              <el-button
                @click="attrVal.attrValueList.splice($index, 1)"
                type="primary"
                size="small"
                icon="Delete"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-button
          type="primary"
          :disabled="attrVal.attrValueList.length == 0"
          @click="confirm"
        >
          保存
        </el-button>
        <el-button type="primary" @click="cancel">取消</el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup lang="ts">
import catalogue from './catalogue.vue'
import { watch, ref, reactive } from 'vue'
import useCateStore from '@/store/modules/cate'
import { reqAttrs, reqSaveAttr, reqDelAttr } from '@/api/pro/att'
import { ElMessage } from 'element-plus'
const screen = ref(0)
const attrs = ref([])
const cateStore = useCateStore()

const attrVal = reactive({
  id: '',
  attrName: '',
  attrValueList: [],
  categoryId: '',
  categoryLevel: 3,
})

watch(
  () => cateStore.c3Id,
  async () => {
    attrs.value = []
    if (!cateStore.c3Id) return

    getAttrs()
  },
)
// 获取属性
const getAttrs = async () => {
  const { c1Id, c2Id, c3Id } = cateStore
  let res = await reqAttrs(c1Id, c2Id, c3Id)
  if (res.code == 200) {
    attrs.value = res.data
  }
}

// 新增属性方法
const insert = () => {
  screen.value = 1
  // 重置值
  Object.assign(attrVal, {
    id: '',
    attrName: '',
    attrValueList: [],
    categoryId: cateStore.c3Id,
    categoryLevel: 3,
  })
}
// 编辑属性方法
const update = (row) => {
  screen.value = 1
  Object.assign(attrVal, row)
}
const removeAll = () => {
  attrVal.attrValueList = []
}
// 取消按钮
const cancel = () => {
  screen.value = 0
  //重新获取
  getAttrs()
}
// 添加一个属性值
const addAttrValue = () => {
  attrVal.attrValueList.push({
    valueName: '',
  })
}

// 删除按钮
const deleteAttr = async (id) => {
  let res = await reqDelAttr(id)
  if (res.code == 200) {
    ElMessage.success('删除成功')
  } else {
    ElMessage.success('删除失败')
  }
  //刷新页面
  getAttrs()
}
// 保存属性值方法
const confirm = async () => {
  //先验证有无重复的属性值
  if (hasDuplicates(attrVal.attrValueList)) {
    ElMessage.error('有重复的属性值或者属性值为空，请您手动删除')
    return
  }
  const res = await reqSaveAttr(attrVal)
  if (res.code == 200) {
    ElMessage.success(attrVal.id != '' ? '编辑成功' : '新增成功')
  } else {
    ElMessage.success(attrVal.id != '' ? '编辑失败' : '新增失败')
  }
  screen.value = 0
  //刷新页面
  getAttrs()
}
//先验证有无重复的属性值
function hasDuplicates(array) {
  for (let i = 0; i < array.length; i++) {
    if (!array[i].valueName) {
      return true
    }
    for (let j = i + 1; j < array.length; j++) {
      if (array[i].valueName === array[j].valueName) {
        return true // 发现重复元素，返回 true
      }
    }
  }
  return false // 没有找到重复元素，返回 false
}
</script>
<style scoped lang="scss"></style>
